package cn.tedu.security.security;

import cn.tedu.security.mapper.UserMapper;
import cn.tedu.security.pojo.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired(required = false)
    UserMapper mapper;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        UserVO userVO = mapper.selectByUserName(s);

        if (userVO!=null) {
            List<GrantedAuthority> authorities =
                    AuthorityUtils.createAuthorityList("USER,ABC".split(","));
            User u = new User(s,userVO.getPassword(),authorities);
            System.out.println("u="+u);
             return u;
        }
        return null;
    }

}
